package com.leetcode;

import org.junit.Test;

/**
 * Created by Z-hong on 2022/9/18.
 */

public class LeetCode827 {

    public static int[][] arr;
    public int largestIsland(int[][] grid) {
        int count=0;
        int max=0;
       arr=new int[grid.length][grid[0].length];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j]==0){
                    getArr(grid);
                    arr[i][j]=1;
                    max=Math.max(max, getMax(i, j, 0));
                    count++;
                }
            }
        }
        if (count==0){return grid.length*grid[0].length;}
        if(count==grid.length*grid[0].length){return 1;}
        return max;
    }

    public int getMax(int i,int j,int count){
        if(i<0||j<0||i>= arr.length||j>=arr[0].length||arr[i][j]==0){return count;}
        count++;
        arr[i][j]=0;
        count=getMax(i+1,j, count);
        count=getMax(i,j+1, count);
        count=getMax(i-1,j, count);
        count=getMax(i,j-1, count);
        return count;
    }

    public void getArr(int[][] grid){
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                arr[i][j]=grid[i][j];
            }
        }
    }

    @Test
    public void test(){
        int [][] arr={{0,0},{0,1}};
        System.out.println(largestIsland(arr));
    }
}
